﻿using KafkaSDK;
using Microsoft.OpenApi.Models;
using OldMvcDemo.BLLs;
using OldMvcDemo.DALs;
using RishCenter.HttpApi.Client;
using Swashbuckle.AspNetCore.Swagger;

namespace OldMvcDemo;

public class Program
{
    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);

        ConfigureServices(builder.Services);

        var app = builder.Build();

        OnInitializeApplication(app, app.Environment);

        app.Run();
    }

    private static void ConfigureServices(IServiceCollection services)
    {
        // Add services to the container.
        //services.AddRazorPages();
        // Add services to the container.
        services.AddSingleton(new CurrentUser
        {
            Id = Guid.Parse("00000000-0000-0000-0000-000000000001")
        });
        services.AddTransient<PaymentBLL>();
        services.AddTransient<AccountDAL>();
        services.AddTransient<RishCheckService>();
        services.AddTransient(typeof(KafkaTemplate<,>));

        services.AddControllers();
        static string GetTypeName(Type type)
        {
            if (type.IsGenericType)
            {
                return $"{type.Name}<{string.Join(',', type.GetGenericArguments().Select(r => GetTypeName(r)))}>";
            }
            else
            {
                return type.Name;
            }
        }
        services.AddSwaggerGen(options =>
        {
            //options.SwaggerDoc("management", new OpenApiInfo { Title = "后台管理接口文档", Version = "v1" });
            options.SwaggerDoc("app", new OpenApiInfo { Title = "应用接口文档", Version = "v1" });
            //options.DocInclusionPredicate((docName, description) => true);
            options.DocInclusionPredicate((docName, description) =>
            {
                //return description.GroupName == docName || description.GroupName != "app" && docName == "management";
                return true;
            });
            options.CustomSchemaIds(GetTypeName);
            options.EnableAnnotations();
        });
    }

    private static void OnInitializeApplication(IApplicationBuilder app, IHostEnvironment environment)
    {
        // Configure the HTTP request pipeline.
        if (!environment.IsDevelopment())
        {
            app.UseExceptionHandler("/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        //app.UseAuthorization();

        //app.MapRazorPages();

        app.UseSwagger(options =>
        {
            options.RouteTemplate = $"/api/doc/{{documentName}}.json";
        });
        //app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Eranntex.EMCS.Web v1"));
        app.UseSwaggerUI(options =>
        {
            options.DocumentTitle = "接口文档";
            //options.RoutePrefix = "api/doc";
            options.SwaggerEndpoint($"/api/doc/app.json", "应用接口文档");
            //options.SwaggerEndpoint($"/{options.RoutePrefix}/management.json", "后台管理接口文档");
        });

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}